package com.medical.Filter;

import com.medical.utils.JwtUtil;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;


@WebFilter(urlPatterns = "/*")
public class TokenFilter implements Filter {
    private static final Logger log = LoggerFactory.getLogger(TokenFilter.class);

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 获取请求路径
        String requestURI = request.getRequestURI();
        log.info("TokenFilter处理请求: {}", requestURI);
        
        // 记录所有请求头，查看是否有token
        java.util.Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                log.info("请求头: {} = {}", headerName, request.getHeader(headerName));
            }
        }
        
        // 判断是否是登录请求
        if (requestURI.contains("/login")) {
            log.info("登录请求,放行");
            filterChain.doFilter(request, response);
            return;
        }
        
        // 获取请求头中的token
        String token = request.getHeader("token");
        log.info("获取到的token: {}", token);
        
        // 判断token是否存在
        if (token == null || token.isEmpty()) {
            log.info("token为空,拦截请求");
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }

        try {
            // 验证token
            log.info("开始验证token");
            JwtUtil.parseToken(token);
            log.info("token验证成功");
        } catch (Exception e) {
            log.error("token验证失败: {}", e.getMessage());
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        }

        log.info("令牌合法,放行");
        filterChain.doFilter(request, response);
    }

/*    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //获取请求路径
        String URI = request.getRequestURI();

        //判断是否是登录请求
        if(URI.contains("/login")){
            filterChain.doFilter(request, response);
            return;
        }

        //获取请求头中的token
        String token = request.getHeader("token");



        //解析token
        try {
            Claims claims = JwtUtils.parseToken(token);
        } catch (Exception e) {
            log.error("token解析失败");
            response.setStatus(401);
            return;
        }

    }*/

}
